import 'package:flutter/material.dart';
import 'package:flutter_axrjc/datasource/network/bean/QianDaoResBean.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/widget/add_notice_dialog/view.dart';
import 'package:flutter_axrjc/widget/count_down_dialog/view.dart';
import 'package:flutter_cupertino_datetime_picker2/flutter_cupertino_datetime_picker2.dart';
import 'package:get/get.dart';

showCommonDialog({
  required String content,
  required GestureTapCallback? onConfirm,
  required GestureTapCallback? onCancel,
  String onConfirmText = "确定",
  String cancelText = "取消",
}) async {
  return await Get.dialog(GestureDetector(
    onTap: Get.back,
    child: Material(
      color: Colors.transparent,
      child: Center(
          child: InkWell(
        onTap: () {},
        child: Container(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(10)),
            width: Get.width * 0.8,
            child: SingleChildScrollView(
              child: Column(
                children: [
                  const Padding(
                    padding: EdgeInsets.symmetric(vertical: 15),
                    child: Text(
                      "提示",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: Text(content),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  const Divider(
                    thickness: 0.3,
                    height: 0.3,
                  ),
                  IntrinsicHeight(
                    child: Row(
                      children: [
                        Expanded(
                            child: InkWell(
                          onTap: onCancel,
                          child: Container(
                            padding: const EdgeInsets.symmetric(vertical: 13),
                            child: Center(
                              child: Text(cancelText),
                            ),
                          ),
                        )),
                        const VerticalDivider(
                          thickness: 0.3,
                          width: 0.3,
                        ),
                        Expanded(
                            child: InkWell(
                          onTap: onConfirm,
                          child: Container(
                            padding: const EdgeInsets.symmetric(vertical: 13),
                            child: Center(
                              child: Text(
                                onConfirmText,
                                style: const TextStyle(color: primaryColor),
                              ),
                            ),
                          ),
                        )),
                      ],
                    ),
                  )
                ],
              ),
            )),
      )),
    ),
  ));
}

bool _isTipDialogShown = false;

Future showTipDialogOnce({
  required String content,
  required GestureTapCallback onConfirm,
  String onConfirmText = "确定",
}) async {
  if (!_isTipDialogShown) {
    _isTipDialogShown = true;
    await Get.dialog(PopScope(
      canPop: false,
      child: Material(
        color: Colors.transparent,
        child: Center(
            child: Container(
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10)),
                width: Get.width * 0.8,
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      const Padding(
                        padding: EdgeInsets.symmetric(vertical: 15),
                        child: Text(
                          "提示",
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 10),
                        child: Text(content),
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      const Divider(
                        thickness: 0.3,
                        height: 0.3,
                      ),
                      InkWell(
                        onTap: () {
                          Get.back();
                          _isTipDialogShown = false;
                          onConfirm();
                        },
                        child: Container(
                          padding: const EdgeInsets.symmetric(vertical: 13),
                          child: Center(
                            child: Text(
                              onConfirmText,
                              style: const TextStyle(color: primaryColor),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                ))),
      ),
    ));
  }
}

showCommonInputDialog({
  required GestureTapCallback? onConfirm,
  required GestureTapCallback? onCancel,
  required String title,
}) async {
  return await Get.dialog(GestureDetector(
    onTap: Get.back,
    child: Material(
      color: Colors.transparent,
      child: Center(
          child: InkWell(
        onTap: () {},
        child: Container(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(10)),
            width: Get.width * 0.8,
            height: 150,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  child: Text(
                    title,
                    style: const TextStyle(fontWeight: FontWeight.bold),
                  ),
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: TextField(
                      style: const TextStyle(fontSize: 12),
                      decoration: InputDecoration(
                          hintText: title,
                          hintStyle: const TextStyle(fontSize: 12),
                          isDense: true,
                          isCollapsed: true,
                          border: InputBorder.none),
                    ),
                  ),
                ),
                const Divider(
                  thickness: 0.3,
                  height: 0.3,
                ),
                IntrinsicHeight(
                  child: Row(
                    children: [
                      Expanded(
                          child: InkWell(
                        onTap: onCancel,
                        child: Container(
                          padding: const EdgeInsets.symmetric(vertical: 13),
                          child: const Center(
                            child: Text("取消"),
                          ),
                        ),
                      )),
                      const VerticalDivider(
                        thickness: 0.3,
                        width: 0.3,
                      ),
                      Expanded(
                          child: InkWell(
                        onTap: onConfirm,
                        child: Container(
                          padding: const EdgeInsets.symmetric(vertical: 13),
                          child: const Center(
                            child: Text(
                              "确定",
                              style: TextStyle(color: primaryColor),
                            ),
                          ),
                        ),
                      )),
                    ],
                  ),
                )
              ],
            )),
      )),
    ),
  ));
}

showQiaoDao(QianDaoResBean? res, String successIntegral, String day) async {
  return await Get.dialog(GestureDetector(
    onTap: Get.back,
    child: Material(
      color: Colors.transparent,
      child: Center(
          child: InkWell(
        onTap: () {},
        child: Container(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(10)),
            width: Get.width * 0.7,
            height: Get.width * 0.7,
            child: Stack(
              children: [
                Positioned.fill(
                    child: Container(
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      gradient: const LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [
                            Color(0xffFFF2CA),
                            Colors.white,
                          ])),
                )),
                Positioned(
                    left: 0,
                    top: 40,
                    child: Image.asset(
                      'assets/main/jifen.png',
                      scale: 3,
                      fit: BoxFit.cover,
                    )),
                Positioned(
                    right: 0,
                    bottom: 20,
                    child: Image.asset(
                      'assets/main/jifen.png',
                      scale: 3,
                      fit: BoxFit.cover,
                    )),
                Column(
                  children: [
                    Expanded(
                      child: res?.status == 'false'
                          ? Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                const Text("今日已签到",
                                    style: TextStyle(fontSize: 24)),
                                const SizedBox(
                                  height: 10,
                                ),
                                Text("已连续签到$day天",
                                    style: const TextStyle(
                                        fontSize: 14,
                                        color: Color(0xff999999))),
                              ],
                            )
                          : Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                const Padding(
                                  padding: EdgeInsets.all(8.0),
                                  child: Text(
                                    "签到成功！",
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 24),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Text(
                                    "+$successIntegral",
                                    style: const TextStyle(
                                        fontSize: 48,
                                        color: orangeColor,
                                        fontWeight: FontWeight.bold),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Text(
                                    "已连续签到${res?.days}天",
                                    style: const TextStyle(
                                        color: Color(0xff999999), fontSize: 14),
                                  ),
                                ),
                              ],
                            ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(15),
                      child: InkWell(
                        onTap: Get.back,
                        child: Container(
                            padding: const EdgeInsets.symmetric(vertical: 10),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(20),
                                gradient: const LinearGradient(
                                    begin: Alignment.topLeft,
                                    end: Alignment.bottomRight,
                                    colors: [
                                      Color(0xffF7D25D),
                                      Color(0xffE3801D),
                                    ])),
                            child: const Center(
                                child: Text(
                              "确定",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 16),
                            ))),
                      ),
                    ),
                  ],
                ),
              ],
            )),
      )),
    ),
  ));
}

showCustomDatePicker({
  DateTime? minDateTime,
  DateTime? maxDateTime,
  DateTime? initialDateTime,
  // String? dateFormat = 'yyyy-M月-d日 H时:m分:s秒',
  String? dateFormat = 'yyyy-M月-d日',
  DateTimePickerLocale locale = DATETIME_PICKER_LOCALE_DEFAULT,
  int pickerMode = 1, //1:datetime ,2:date  ,否则:time
  DateTimePickerTheme pickerTheme = DateTimePickerTheme.Default,
  Function? onCancel,
  Function? onClose,
  Function(DateTime, List<int>)? onChange,
  Function(DateTime, List<int>)? onConfirm,
  int minuteDivider = 1,
  bool onMonthChangeStartWithFirstDate = false,
}) {
  var mode;
  if (pickerMode == 1) {
    mode = DateTimePickerMode.datetime;
  } else if (pickerMode == 2) {
    mode = DateTimePickerMode.date;
  } else {
    mode = DateTimePickerMode.time;
  }
  DatePicker.showDatePicker(Get.context!,
      minDateTime: minDateTime,
      maxDateTime: maxDateTime,
      initialDateTime: initialDateTime,
      dateFormat: dateFormat,
      pickerMode: mode,
      pickerTheme: const DateTimePickerTheme(
          cancel: Text(
            "取消",
            style: TextStyle(
                fontSize: 16, fontWeight: FontWeight.bold, color: orangeColor),
          ),
          confirm: Text(
            "确认",
            style: TextStyle(
                fontSize: 16, fontWeight: FontWeight.bold, color: orangeColor),
          )), onCancel: () {
    onCancel?.call();
  }, onClose: () {
    onClose?.call();
  }, onChange: (time, list) {
    onChange?.call(time, list);
  }, onConfirm: (time, list) {
    onConfirm?.call(time, list);
  });
}

showAddNoticeDialog() async {
  return await Get.dialog(const AddNoticeDialogPage(),
      barrierDismissible: false);
}

/// 跳转图片预览
showPicPreview(List<String> picList, {int? position}) async {
  var map = {"urlList": picList, "position": position};
  print(map);
  return await Get.toNamed(MainRoutes.picPreview, arguments: map);
}

showCountDown(int seconds) async {
  return await Get.dialog(
      CountDownDialogView(duration: Duration(seconds: seconds)),
      barrierDismissible: false);
}
